//
//  FZEmoticonView.swift
//  FZSina_Swift
//
//  Created by 李忠 on 7/30/16.
//  Copyright © 2016 FZLZ. All rights reserved.
//

import UIKit

class FZEmoticonKeyboardView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 0, width: SCREENW, height: 216))
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI() {
        
        self.backgroundColor = UIColor.init(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
        addSubview(toolBar)
        addSubview(pageView)
        
        toolBar.snp_makeConstraints { (make) in
            make.leading.bottom.trailing.equalTo(self)
            make.height.equalTo(30)
        }
        pageView.snp_makeConstraints { (make) in
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(self.toolBar.snp_top)
        }
    }
    
    //表情键盘
    lazy var pageView: FZEmoticonPageView = {
        let layout = UICollectionViewFlowLayout()
        let page = FZEmoticonPageView(frame: CGRectZero, collectionViewLayout: layout)
        
        page.backgroundColor = self.backgroundColor
        page.pagingEnabled = true
        page.showsHorizontalScrollIndicator = false
        page.showsVerticalScrollIndicator = false
        page.bounces = false
        page.delegate = self
        return page
    }()
    //选择标签标签的按钮
    lazy var toolBar: FZEmoticonToolBarView = {
        let tool = FZEmoticonToolBarView(frame: CGRectZero)
        tool.clickButonClosure = {[weak self] (type: FZEmoticonToolBarType) in
            
            let indexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            
            self?.pageView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            
        }
        return tool
    }()
}

extension FZEmoticonKeyboardView: UICollectionViewDelegate{
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        let moveX = scrollView.contentOffset.x + SCREENW * 0.5
        let curCenter = CGPoint(x: moveX, y: 0)
        
        //visibleCells()屏幕可见的cell
        let visibles = pageView.visibleCells().sort({ (cell1, cell2) -> Bool in
            cell1.frame.origin.x < cell2.frame.origin.x
        })
        
        if visibles.count != 2 {
            return
        }
        
        let fistCell = visibles.first! as! FZEmoticonPageCell
        let lastCell = visibles.last! as! FZEmoticonPageCell
        
        var indexPath: NSIndexPath = NSIndexPath()
        
        if fistCell.frame.contains(curCenter) {
            
            indexPath = pageView.indexPathForCell(fistCell)!
        }else if lastCell.frame.contains(curCenter) {
            
            indexPath = pageView.indexPathForCell(lastCell)!
        }
        
        toolBar.selectEmonticonButton(FZEmoticonToolBarType(rawValue: indexPath.section)!)
        
    }
}
